package com.twu.biblioteca;

import org.junit.Test;
import org.junit.Before;
import org.junit.After;

import java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Created by jfulton on 16/01/2014.
 */
public class Bibliotesta {

    private ItemManager<Book> bookManager;
    private ItemManager<Movie> movieManager;
    private String testBookTitle = "20,000 Leagues Under the Sea";
    private Movie testMovie;

    @Before
    public void init() {
        bookManager = new ItemManager<Book>();
        movieManager = new ItemManager<Movie>();
        testMovie = new Movie("Test", "2000", "John Smith", 0);
        bookManager.addItem(new Book(testBookTitle), 1);
        movieManager.addItem(testMovie, 1);
    }

    @Test
    public void testCheckoutValid() throws Exception {
        bookManager.checkoutItem(testBookTitle);
    }

    @Test(expected = ItemUnavailableException.class)
    public void testCheckoutInvalid() throws Exception {
        bookManager.checkoutItem("20,000 Lawyers Under the Sea");
    }

    @Test
    public void testGetAvailableBooks() throws Exception {
        Set<Book> booksAvailable = bookManager.getAvailableItems();
        Book testBook = bookManager.findItemByTitle(testBookTitle);
        assertTrue(booksAvailable.contains(testBook));
        assertEquals(1, booksAvailable.size());
    }

    @Test
    public void testGetAvailableBooksAfterCheckout() throws Exception {
        bookManager.checkoutItem(testBookTitle);
        Book testBook = bookManager.findItemByTitle(testBookTitle);
        Set<Book> booksAvailable = bookManager.getAvailableItems();
        assertTrue(!booksAvailable.contains(testBook));
    }

    @Test(expected = CannotReturnItemException.class)
    public void testInvalidBookReturn() throws Exception {
        bookManager.returnItem("The Cat on the Mat");
    }

    @Test
    public void testValidBookReturn() throws Exception {
        bookManager.checkoutItem(testBookTitle);
        bookManager.returnItem(testBookTitle);

        Set<Book> booksAvailable = bookManager.getAvailableItems();
        Book testBook = bookManager.findItemByTitle(testBookTitle);
        assertTrue(booksAvailable.contains(testBook));
    }

    @After
    public void cleanup() {

    }

}
